﻿using HikVisionTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HikVisionTool.DAL
{
    public class HikVisonDAL
    {
        public Sql2Helper sql2Helper = new Sql2Helper();
        public Sql3Helper sql3Helper = new Sql3Helper();
        public Sql5Helper sql5Helper = new Sql5Helper();
        public Sql10Helper sql10Helper = new Sql10Helper();

        public bool ChangStatus(Sql3Helper.t_HK_workorder_barcode_status status)
        {
            try
            {
                Sql3Helper.t_HK_workorder_barcode_status oldStatus = sql3Helper.db.Queryable<Sql3Helper.t_HK_workorder_barcode_status>().Where(it => it.barcd == status.barcd).First();
                if (oldStatus != null)
                {
                    oldStatus.chname = status.chname;
                    oldStatus.chtime = status.chtime;
                    oldStatus.arrivetime = status.arrivetime;
                    oldStatus.next_pline = status.next_pline;
                    oldStatus.next_processid = status.next_processid;
                    oldStatus.status = status.status;
                    oldStatus.processid = status.processid;
                    int result = sql3Helper.db.Updateable(oldStatus).ExecuteCommand();
                }
                //if (result!=1)
                // {
                //     return false;
                // }
                return true;
            }
            catch (Exception ex)
            {
                FileHelper.WriteLog(ex.ToString());
                return true;
            }

        }
        public List<Sql5Helper.t_org_tree> GetIPCLines()
        {
            try
            {
                List<string> lines = new List<string>();
                return sql5Helper.db.Queryable<Sql5Helper.t_org_tree>().Where(it =>it.parent_id == 167).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Sql5Helper.t_org_tree> GetTHTLines()
        {
            try
            {
                List<string> lines = new List<string>();
                return sql5Helper.db.Queryable<Sql5Helper.t_org_tree>().Where(it => it.parent_id == 166).ToList();
                //List<string> lines = new List<string>();
                //return sql2Helper.db.Queryable<Sql2Helper.t_Item>().Where(it => it.itemName.Contains("测试") && it.pid == 54).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public string GetWkNum(string barCode)
        {
            try
            {
                return sql2Helper.db.Queryable<Sql2Helper.Order_Match>().Where(it => it.prodBarCode == barCode).First().order_code;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string CheckQiMi(string barCode, string line)
        {
            try
            {
                Sql2Helper.hkwsAll hkwsAll = sql2Helper.db.Queryable<Sql2Helper.hkwsAll>().Where("code1='" + barCode + "'").First();
                if (hkwsAll == null)
                {
                    return "上一站测试未通过!";
                }
                if (hkwsAll.code4 != barCode)
                {
                    return "上一站测试未通过!";
                }
                if (line.Replace("IPC成品组装", "").Equals(hkwsAll.line) == false)
                {
                    return "非本线产品!";
                }
                return "OK";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string CheckZhuangPei(string barCode, string line)
        {
            try
            {
                Sql2Helper.hkwsAll hkwsAll = sql2Helper.db.Queryable<Sql2Helper.hkwsAll>().Where("code1='" + barCode + "'").First();
                if (hkwsAll == null)
                {
                    return "上一站测试未通过!";
                }
                if (hkwsAll.code1 != barCode)
                {
                    return "上一站测试未通过!";
                }
                if (line.Replace("IPC成品组装", "").Equals(hkwsAll.line) == false)
                {
                    return "非本线产品!";
                }
                return "OK";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool UpHDGuoZhan(string barCode, string name)
        {
            try
            {
                string time = sql2Helper.db.GetDate().ToString();
                int result = sql2Helper.db.Updateable<Sql2Helper.hkwsAll>()
                    .SetColumns(it => new Sql2Helper.hkwsAll() { code2 = barCode, code3 = barCode, name2 = name, name3 = name, time2 = time, time3 = time, thd = 1, Sec = 1 })
                    .Where(it => it.code1 == barCode).ExecuteCommand();
                if (result != 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public Sql5Helper.order_work GetPlanMain(string order)
        {
            try
            {
                return sql5Helper.db.Queryable<Sql5Helper.order_work>().Where(it => it.order_prod_number == order).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool UpTHTGuoZhan(Sql2Helper.hkws hkws)
        {
            try
            {
                int result = 0;

                Sql2Helper.hkws model = sql2Helper.db.Queryable<Sql2Helper.hkws>().Where(it => it.prodBarCode == hkws.prodBarCode).First();
                if (model != null)
                {
                    model.MoCode = hkws.MoCode;
                    model.id_wo = hkws.id_wo;
                    model.plcode = hkws.plcode;
                    model.Jiwei = hkws.Jiwei;
                    model.Gongwei = hkws.Gongwei;
                    model.ProduceModel = hkws.ProduceModel;
                    model.createTime = hkws.createTime;
                    result = sql2Helper.db.Updateable(model).ExecuteCommand();
                }
                else
                {
                    result = sql2Helper.db.Insertable(hkws).ExecuteCommand();
                }
                if (result != 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool UpTHTGuoZhan2(Sql2Helper.hkws hkws)
        {
            try
            {
                int result = 0;
                Sql10Helper.hkws model = sql10Helper.db.Queryable<Sql10Helper.hkws>().Where(it => it.prodBarCode == hkws.prodBarCode).First();
                if (model != null)
                {
                    model.id_wo = hkws.id_wo;
                    model.plcode = hkws.plcode;
                    model.Jiwei = hkws.Jiwei;
                    model.Gongwei = hkws.Gongwei;
                    model.createTime = hkws.createTime;
                    result = sql10Helper.db.Updateable(model).ExecuteCommand();
                }
                else
                {
                    model = new Sql10Helper.hkws()
                    {
                        id_wo = hkws.id_wo,
                        plcode = hkws.plcode,
                        Jiwei = hkws.Jiwei,
                        Gongwei = hkws.Gongwei,
                        prodBarCode = hkws.prodBarCode,
                        createTime = hkws.createTime
                    };
                    result = sql10Helper.db.Insertable(model).ExecuteCommand();
                }
                if (result != 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool UpPlanDetail(int id_main, string dateTime)
        {
            try
            {
                int result = 0;
                Sql2Helper.t_proc_plan_detail model = sql2Helper.db.Queryable<Sql2Helper.t_proc_plan_detail>().
                    Where(it => it.plandate == dateTime && it.type_plan == 3 && it.type_produce == 4 && it.id_main == id_main).First();
                if (model != null)
                {
                    model.count = model.count + 1;
                    result = sql2Helper.db.Updateable(model).ExecuteCommand();
                }
                else
                {
                    model = new Sql2Helper.t_proc_plan_detail()
                    {
                        id_main = id_main,
                        type_plan = 3,
                        plandate = dateTime,
                        type_produce = 4,
                        count = 1
                    };
                    result = sql2Helper.db.Insertable(model).ExecuteCommand();
                }
                if (result < 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
